package com.example.demo.spring;

import com.example.demo.Constants;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * Bean名称映射控制器<br>
 * 参考：https://fangshixiang.blog.csdn.net/article/details/89845181
 *
 * @author Yosemite
 * @date 2021/11/13
 */
@org.springframework.stereotype.Controller("/controller/beanNameMapping")// 注意此处需要以/开头，表示使用BeanNameURLHandlerMapping的方式处理
public class BeanNameMappingController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html");

        // Hello
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>" + String.format(Constants.HELLO_TEMPLATE, "BeanNameMappingController") + "</h1>");
        out.println("</body></html>");

        // 返回null告诉视图渲染  直接把body里面的内容输出浏览器即可
        return null;
    }
}
